package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.FaceDraft;
import com.yami.shop.bean.model.FaceDesignOrder;
import com.yami.shop.dao.FaceDraftMapper;
import com.yami.shop.service.FaceDraftService;
import com.yami.shop.service.FaceDesignOrderService;
import com.yami.shop.common.util.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 草稿单服务实现
 * @author AI Assistant
 */
@Service
public class FaceDraftServiceImpl extends ServiceImpl<FaceDraftMapper, FaceDraft> implements FaceDraftService {


    @Autowired
    private FaceDraftMapper faceDraftMapper;

    @Autowired
    @Lazy
    private FaceDesignOrderService faceDesignOrderService;


    @Override
    public List<FaceDraft> listByUserIdAndStatus(String userId, Integer status) {
        LambdaQueryWrapper<FaceDraft> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FaceDraft::getUserId, userId);
        if (status != null) {
            queryWrapper.eq(FaceDraft::getDraftStatus, status);
        }
        queryWrapper.orderByDesc(FaceDraft::getCreateTime);
        return faceDraftMapper.selectList(queryWrapper);
    }

    @Override
    public Integer countByUserIdAndStatus(String userId, Integer status) {
        LambdaQueryWrapper<FaceDraft> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FaceDraft::getUserId, userId);
        if (status != null) {
            queryWrapper.eq(FaceDraft::getDraftStatus, status);
        }
        return Math.toIntExact(faceDraftMapper.selectCount(queryWrapper));
    }

    @Override
    public void updateStatus(Long draftId, Integer status) {
        FaceDraft faceDraft = new FaceDraft();
        faceDraft.setDraftId(draftId);
        faceDraft.setDraftStatus(status);
        faceDraftMapper.updateById(faceDraft);
    }

    @Override
    public void saveFaceDraft(FaceDraft faceDraft) {
        faceDraftMapper.insert(faceDraft);
    }

    @Override
    public void updateFaceDraft(FaceDraft faceDraft) {
        faceDraftMapper.updateById(faceDraft);
    }

    @Override
    public void deleteFaceDraft(Long draftId) {
        faceDraftMapper.deleteById(draftId);
    }

    @Override
    public IPage<FaceDraft> pageByUserIdAndStatus(PageParam<FaceDraft> page, String userId, Integer status) {
        return faceDraftMapper.selectPage(page, new LambdaQueryWrapper<FaceDraft>()
                .eq(userId != null, FaceDraft::getUserId, userId)
                .eq(status != null, FaceDraft::getDraftStatus, status)
                .orderByDesc(FaceDraft::getCreateTime));
    }

    @Override
    public void convertToDesign(Long draftId, String userId) {
        FaceDraft faceDraft = faceDraftMapper.selectById(draftId);
        if (faceDraft == null) {
            throw new RuntimeException("草稿单不存在");
        }
        if (!faceDraft.getUserId().equals(userId)) {
            throw new RuntimeException("没有权限操作该草稿单");
        }

        // 更新草稿状态为已保存为设计稿，设置排单状态为未排单
        FaceDraft updateDraft = new FaceDraft();
        updateDraft.setDraftId(draftId);
        updateDraft.setDraftStatus(2); // 已保存为设计稿
        updateDraft.setScheduleStatus(0); // 未排单
        faceDraftMapper.updateById(updateDraft);
    }

    @Override
    public void applySchedule(Long draftId, String userId) {
        FaceDraft faceDraft = faceDraftMapper.selectById(draftId);
        if (faceDraft == null) {
            throw new RuntimeException("草稿单不存在");
        }
        if (!faceDraft.getUserId().equals(userId)) {
            throw new RuntimeException("没有权限操作该草稿单");
        }
        if (faceDraft.getDraftStatus() != 2) {
            throw new RuntimeException("只有已保存为设计稿的草稿单才能申请排单");
        }
        if (faceDraft.getScheduleStatus() != 0) {
            throw new RuntimeException("只有未排单的设计稿才能申请排单");
        }

        // 更新排单状态为待确认排单
        FaceDraft updateDraft = new FaceDraft();
        updateDraft.setDraftId(draftId);
        updateDraft.setUpdateTime(new Date());
        updateDraft.setScheduleStatus(1); // 待确认排单
        faceDraftMapper.updateById(updateDraft);
    }

    @Override
    public Long confirmSchedule(Long draftId, java.math.BigDecimal orderAmount) {
        FaceDraft faceDraft = faceDraftMapper.selectById(draftId);
        if (faceDraft == null) {
            throw new RuntimeException("草稿单不存在");
        }
        if (faceDraft.getScheduleStatus() != 1) {
            throw new RuntimeException("只有待确认排单的设计稿才能确认排单");
        }

        // 调用FaceDesignOrderService创建实际的订单记录
        FaceDesignOrder order = faceDesignOrderService.createOrder(
            faceDraft.getUserId(),
            null, // designId暂时为null，因为是从草稿直接创建订单
            draftId,
            orderAmount
        );

        Long orderId = order.getOrderId();

        // 更新排单状态为已确认排单，并关联订单ID
        FaceDraft updateDraft = new FaceDraft();
        updateDraft.setDraftId(draftId);
        updateDraft.setScheduleStatus(2); // 已确认排单
        updateDraft.setOrderId(orderId);
        updateDraft.setUpdateTime(new Date());
        faceDraftMapper.updateById(updateDraft);

        return orderId;
    }

    @Override
    public List<FaceDraft> listByScheduleStatus(String userId, Integer scheduleStatus) {
        LambdaQueryWrapper<FaceDraft> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(userId != null, FaceDraft::getUserId, userId)
                   .eq(scheduleStatus != null, FaceDraft::getScheduleStatus, scheduleStatus)
                   .orderByDesc(FaceDraft::getCreateTime);
        return faceDraftMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<FaceDraft> pageByStatus(PageParam<FaceDraft> page, Long userId, Integer draftStatus, Integer scheduleStatus) {
        LambdaQueryWrapper<FaceDraft> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(userId != null, FaceDraft::getUserId, userId)
                   .eq(draftStatus != null, FaceDraft::getDraftStatus, draftStatus)
                   .eq(scheduleStatus != null, FaceDraft::getScheduleStatus, scheduleStatus)
                   .orderByDesc(FaceDraft::getCreateTime);
        return faceDraftMapper.selectPage(page, queryWrapper);
    }
}
